package com.ricchieri.manage.bean;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.EmailTemplateFilter;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "editEmailMB")
@ViewScoped
public class EditEmailTemplateManageBean extends CommonEmailTemplateManageBean {

	private static final long serialVersionUID = -8201281010249836515L;
	
	private EmailTemplateFilter emailTemplateFilter;
	
	@PostConstruct
	public void init() {
		super.init();
		emailTemplateFilter = (EmailTemplateFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
	}	
	
	public String getEmailTemplateType() {
		if(this.emailTemplateType == null) {
			this.emailTemplateType = String.valueOf(getEmailTemplate().getEmailToSendType().getId());
		}
		
		return emailTemplateType;
	}
	
	@Override
	public String save() {
		getEmailTemplate().setEmailToSendType(this.obtainEmailType(getEmailTemplateType()));
		emailTemplateService.updateTemplate(getEmailTemplate());
		FacesUtil.addParametersFlash(BusinessSettings.KEY_EMAIL_TEMPLATE, getEmailTemplate());
		
		return BusinessSettings.VIEW_DETAILS;
	}

	@Override
	public Boolean isRequeriedEmailTemplate() {
		return Boolean.TRUE;
	}

	public String doBackToSearchEmailTemplate() {		
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, emailTemplateFilter);

		return BusinessSettings.SEARCH_EMAIL_TEMPLATE;
	}	
}
